package mlp.game.applebucking;


import com.overkill.game.RenderView;
import com.overkill.game.Tools;
import com.playtomic.android.api.Playtomic;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class IntroActivity extends Activity {
	public static final String URL_TWITTER = "https://twitter.com/OV3RK1LL";
	public static final String URL_FACEBOOK = "http://www.facebook.com/PonyGameCollection";
	public static final String URL_PAYPAL = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UYQJSSAKZNMFY";

	private boolean backToQuit = true;
	
	MediaPlayer mp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);
		try {
	    	 Playtomic.getInstance(
	    			 5894, 
	    			 "14c95cc4d02545c5", 
	    			 "cbad97ed3767409ab8026ab548c280", 
	    			 this);
	    	 Playtomic.Log().view();
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }	  
		Typeface celestia = Typeface.createFromAsset(getAssets(), RenderView.TYPEFACE_PATH);
		Tools.setTypeFace(this, celestia, R.id.btnStart, R.id.btnHelp, R.id.btnBack, R.id.btnEasy, R.id.btnHard, R.id.txtAbout, R.id.txtWelcome, R.id.txtAnd, R.id.txtPresent, R.id.btnTwitter, R.id.btnDonate, R.id.btnFacebook);

		mp = MediaPlayer.create(IntroActivity.this, R.raw.intro);
		mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mp.start();
		playIntro();

	}

	public void playIntro() {
		// This animation does not change anything but forces the view to be redrawn.
		// The background TranslateAnimation gets stuck if nothing is redrawn in the foreground
		TextView t = (TextView)findViewById(R.id.txtAbout);
		AlphaAnimation a = new AlphaAnimation(1, 1);
		a.setDuration(100);
		a.setRepeatCount(Animation.INFINITE);
		a.setRepeatMode(Animation.RESTART);
		t.startAnimation(a);		
		
		// Reload the background image to rescale
		ImageView intro = (ImageView) findViewById(R.id.intoAnimation);
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.background);
		intro.setImageBitmap(bg);
		// Start animation
		TranslateAnimation move = new TranslateAnimation(0, metrics.widthPixels	- bg.getWidth(), 0, 0);
		move.setDuration(10000);
		move.setFillEnabled(true);
		move.setFillAfter(true);
		move.setZAdjustment(Animation.ZORDER_BOTTOM);
		intro.startAnimation(move);

		showViewThenHide(findViewById(R.id.aboutLayout), 1000, 1000, 3000);

		showMenu(6500);
	}

	@Override
	protected void onDestroy() {
		mp.stop();
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if(backToQuit){
			super.onBackPressed();
		}else{
			View v = new View(this);
			v.setId(R.id.btnBack);
			onStartClick(v);			
		}
	}
	
	public void onStartClick(View view) {
		Intent i = null;
		switch(view.getId()){
		case R.id.btnStart:
			((TableRow)findViewById(R.id.tableRow3)).setVisibility(View.GONE);
			((TableRow)findViewById(R.id.tableRow4)).setVisibility(View.GONE);
			((TableRow)findViewById(R.id.tableRow5)).setVisibility(View.GONE);

			((TableRow)findViewById(R.id.tableRow6)).setVisibility(View.VISIBLE);
			((TableRow)findViewById(R.id.tableRow7)).setVisibility(View.VISIBLE);
			((TableRow)findViewById(R.id.tableRow8)).setVisibility(View.VISIBLE);
			
			backToQuit = false;
			break;
		case R.id.btnBack:
			((TableRow)findViewById(R.id.tableRow3)).setVisibility(View.VISIBLE);
			((TableRow)findViewById(R.id.tableRow4)).setVisibility(View.VISIBLE);
			((TableRow)findViewById(R.id.tableRow5)).setVisibility(View.VISIBLE);

			((TableRow)findViewById(R.id.tableRow6)).setVisibility(View.GONE);
			((TableRow)findViewById(R.id.tableRow7)).setVisibility(View.GONE);
			((TableRow)findViewById(R.id.tableRow8)).setVisibility(View.GONE);
			
			backToQuit = true;
			break;
		case R.id.btnEasy:
			i = new Intent(this, GameActivity.class);
			i.putExtra("difficulty", "EASY");
			break;
		case R.id.btnHard:
			i = new Intent(this, GameActivity.class);
			i.putExtra("difficulty", "HARD");
			break;
		}
		if(i != null){
			i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			overridePendingTransition(0, 0);
			finish();
			overridePendingTransition(0, 0);
			Playtomic.Log().playerLevelStart(i.getExtras().getString("difficulty"));
			startActivity(i);
		}
		
	}

	public void onHelpClick(View view){		
		Playtomic.Log().customMetric("Help",  "Screens", false); 	
		new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_info)
			.setTitle(R.string.help_dialog_title)
			.setMessage(R.string.help_dialog_message)
			.setPositiveButton(android.R.string.yes, null)
			.show();
	}
	
	public void onExtraClick(View view) {
		Intent i = null;
		switch (view.getId()) {
		case R.id.btnDonate:
			i = new Intent(Intent.ACTION_VIEW, Uri.parse(URL_PAYPAL));
			break;
		case R.id.btnFacebook:
			i = new Intent(Intent.ACTION_VIEW, Uri.parse(URL_FACEBOOK));
			break;
		case R.id.btnTwitter:
			i = new Intent(Intent.ACTION_VIEW, Uri.parse(URL_TWITTER));
			break;
		default:
			break;
		}
		if(i != null)
			startActivity(i);
	}
	
	public void onBackgroundClick(View view) {
		mp.stop();
		hideView(findViewById(R.id.aboutLayout), 0, 1000);
		showMenu(0);
	}

	public void showMenu(int delay) {
		LinearLayout l = (LinearLayout) findViewById(R.id.menuLayout);
		showView(l, delay, 2000);
	}

	public void showView(final View view, int delay, int duration) {
		if (view.getVisibility() == View.VISIBLE)
			return;
		view.clearAnimation();
		view.setVisibility(View.INVISIBLE);
		AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
		fadeIn.setStartOffset(delay);
		fadeIn.setDuration(duration);
		fadeIn.setFillAfter(true);
		fadeIn.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				view.setVisibility(View.VISIBLE);
			}
		});
		view.startAnimation(fadeIn);
	}

	public void showViewThenHide(final View view, final int delay, final int fadeDuration, final int visibleDuration) {
		if (view.getVisibility() == View.VISIBLE)
			return;
		view.clearAnimation();
		view.setVisibility(View.INVISIBLE);
		AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
		fadeIn.setStartOffset(delay);
		fadeIn.setDuration(fadeDuration);
		fadeIn.setFillAfter(true);
		fadeIn.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				view.setVisibility(View.VISIBLE);
				AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
				fadeOut.setStartOffset(visibleDuration);
				fadeOut.setDuration(fadeDuration);
				fadeOut.setFillBefore(false);
				fadeOut.setFillAfter(true);
				fadeOut.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						view.setVisibility(View.GONE);
					}
				});
				view.startAnimation(fadeOut);
			}
		});
		view.startAnimation(fadeIn);
	}

	public void hideView(final View view, int delay, int duration) {
		if (view.getVisibility() == View.GONE)
			return;
		view.clearAnimation();
		AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
		fadeOut.setStartOffset(delay);
		fadeOut.setDuration(duration);
		fadeOut.setFillBefore(false);
		fadeOut.setFillAfter(true);
		fadeOut.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				view.setVisibility(View.GONE);
			}
		});
		view.startAnimation(fadeOut);
	}
}
